package com.gylang.im.socket.process.controller;

import cn.hutool.core.util.StrUtil;
import com.gylang.im.core.enums.AuthEnums;
import com.gylang.netty.sdk.MessageProvider;
import com.gylang.netty.sdk.annotation.NettyHandler;
import com.gylang.netty.sdk.constant.MessageType;
import com.gylang.netty.sdk.domain.MessageWrap;
import com.gylang.netty.sdk.domain.model.IMSession;
import com.gylang.netty.sdk.handler.NettyController;
import com.gylang.netty.sdk.repo.IMSessionRepository;
import com.gylang.user.context.domain.UserContextDTO;
import com.gylang.user.context.util.UserContextUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 单用户登录 多设备多端还未实现
 *
 * @author gylang
 * data 2020/11/25
 * @version v0.0.1
 */
@NettyHandler("login")
@Component
public class LoginController implements NettyController<String> {

    @Resource
    private MessageProvider messageProvider;
    @Resource
    private IMSessionRepository sessionReopository;
    @Override
    public void process(IMSession me, String accessToken) {

        UserContextDTO userContext = UserContextUtils.getUserContext(accessToken);

            MessageWrap messageWrap = new MessageWrap();
            messageWrap.setType(MessageType.NOTIFY);
        if (null == userContext || StrUtil.isBlank(userContext.getUid())) {
            messageWrap.setContent(AuthEnums.USER_NOT_LOGIN.getMsg());
            messageWrap.setCode(AuthEnums.USER_NOT_LOGIN.getCode());
            messageProvider.sendMsg(me, me, messageWrap);
            return;
        }
        me.setAccount(userContext.getUid());
        sessionReopository.add(userContext.getUid(), me);
        messageWrap.setContent("成功");
        messageWrap.setCode("00000");
    }
}
